<?php
# http://projects:8081/cdapp-2kt-api/api/contacts_get.php

include_once '_portal.php';
// include_once '_mango.php';
// const COLNAME = 'contacts';
forbidden_acc();

$action   = isset($post->action) ? $post->action : null;
$contact  = isset($post->contact) ? $post->contact : null;
$tag      = isset($post->tag) ? $post->tag : null;
$uid      = isset($post->uid) ? $post->uid : null;
$pid      = isset($post->pid) ? $post->pid : null;
$nonlocal = isset($post->nonlocal) ? $post->nonlocal : false;
// $oid = isset($post->oid) ? $post->oid : null;

if ($action == 'add') {
  $json_tags   = json_stringify($contact->tags);
  $json_phones = json_stringify($contact->phones);
  $sql         = "INSERT INTO 2kt_contacts (name,tags,phones) VALUES ('{$contact->name}','{$json_tags}','{$json_phones}')";
  $result      = exec_sql($sql);
  output(['result' => $result, 'sql' => $sql]);
  // $rsult = output(mongo_insert(COLNAME, [$contact]));
  die();
} elseif ($action == 'modify') {
  $json_tags   = json_stringify($contact->tags);
  $json_phones = json_stringify($contact->phones);
  $sql         = "UPDATE `2kt_contacts` SET `name`='{$contact->name}', `tags`='{$json_tags}',`phones`='{$json_phones}' WHERE `uid`={$contact->uid}";
  $result      = exec_sql($sql);
  output(['result' => $result, 'sql' => $sql]);
  // output(mongo_update(COLNAME, ['_id' => mongo_oid($oid)], ['$set' => ['name' => $contact->name, 'tags' => $contact->tags, 'phones' => $contact->phones]]));
  die();
} elseif ($action == 'remove') {
  $sql    = "DELETE FROM `2kt_contacts` WHERE `uid` = {$uid}";
  $result = exec_sql($sql);
  output(['result' => $result, 'sql' => $sql]);
  // output(mongo_delete(COLNAME, ["_id" => mongo_oid($oid)]));
  die();
} elseif ($action == 'local') {
  $sql    = "UPDATE `2kt_contacts` SET `phones` = JSON_SET(`phones`,'$[{$pid}].nonlocal',{$nonlocal}) WHERE `uid` = {$uid}";
  $result = exec_sql($sql);
  output(['result' => $result, 'sql' => $sql]);
} elseif ($action == 'tag') {
  // output(mongo_query(COLNAME, ["tags" => $tag], []));
  die();
} else {
  $sql    = "SELECT * FROM `2kt_contacts` ORDER BY CONVERT(`name` USING gbk)";
  $result = exec_sql($sql);
  output(query_parse($result));
  // output(mongo_query(COLNAME));
  die();
}

// $docs = [
//   [
//     "name" => "王涛",
//     "tags" => [
//       "终端管制室",
//     ],
//     "phones" => [
//       [
//         "title" => "手机",
//         "number" => "18628205151",
//         "nonlocal" => false,
//       ],
//     ],
//   ],
// ];

// $result0 = mongo_insert(COLNAME, $docs);

// $filter = ['name' => '王涛'];
// $update = ['$push' => ['phones' => ['title' => 'cell', 'number' => '110']]];
// $result1 = mongo_update(COLNAME, $filter, $update);

// $filter = [];
// $options = [];
// $result2 = mongo_query(COLNAME, $filter, $options);

// output(['result1' => $result1, 'result2' => $result2]);

// $filter = [
//   "phones.number" => '110',
//   "phones" => ['$elemMatch' => ['number' => '110']],
//   "tags" => ['$elemMatch' => ['$eq' => '终端管制室']],
//   "tags" => '终端管制室',
// ];

// $options = [];
// $result = mongo_query(COLNAME, $filter, $options);
// output(['result'=>$result,'sql'=>$sql]);

// $filter = ["phones.number" => '110'];
// $result = mongo_delete(COLNAME, $filter);
